

<!DOCTYPE html>
<html lang="zh-CN" data-default-color-scheme=dark>



<head>
  <meta charset="UTF-8">
  <link rel="apple-touch-icon" sizes="76x76" href="/my-blog/img/fluid.png">
  <link rel="icon" href="/my-blog/img/fluid.png">
  <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=5.0, shrink-to-fit=no">
  <meta http-equiv="x-ua-compatible" content="ie=edge">
  
  <meta name="theme-color" content="#2f4154">
  <meta name="author" content="木舟">
  <meta name="keywords" content="python Vue Test 测试开发 自动化测试">
  
    <meta name="description" content="前言Javascript存在的最大意义，就是用来控制页面行为。 通过编写JavaScript代码，来实现页面的交互、数据的展示。 在如今三大前端框架的出现，通过JavaScript直接操作Dom的场景变得越来越少，目前在实际开发中的场景一般为：  echarts图表渲染 创建a标签进行资源下载 自适应高度 事件监听  而Dom并不是JavaScript语言本身的东西，而是浏览器提供给JavaScr">
<meta property="og:type" content="article">
<meta property="og:title" content="JavaScript Dom操作">
<meta property="og:url" content="https://walker_kwu.gitee.io/my-blog/2022/10/29/notes/js/dom/">
<meta property="og:site_name" content="My Blog">
<meta property="og:description" content="前言Javascript存在的最大意义，就是用来控制页面行为。 通过编写JavaScript代码，来实现页面的交互、数据的展示。 在如今三大前端框架的出现，通过JavaScript直接操作Dom的场景变得越来越少，目前在实际开发中的场景一般为：  echarts图表渲染 创建a标签进行资源下载 自适应高度 事件监听  而Dom并不是JavaScript语言本身的东西，而是浏览器提供给JavaScr">
<meta property="og:locale" content="zh_CN">
<meta property="article:published_time" content="2022-10-29T08:55:27.000Z">
<meta property="article:modified_time" content="2023-05-23T10:37:53.254Z">
<meta property="article:author" content="木舟">
<meta property="article:tag" content="测试开发">
<meta property="article:tag" content="测试平台">
<meta property="article:tag" content="javascript">
<meta name="twitter:card" content="summary_large_image">
  
  
    <meta name="referrer" content="no-referrer-when-downgrade">
  
  
  <title>JavaScript Dom操作 - My Blog</title>

  <link  rel="stylesheet" href="https://lib.baomitu.com/twitter-bootstrap/4.6.1/css/bootstrap.min.css" />



  <link  rel="stylesheet" href="https://lib.baomitu.com/github-markdown-css/4.0.0/github-markdown.min.css" />

  <link  rel="stylesheet" href="https://lib.baomitu.com/hint.css/2.7.0/hint.min.css" />

  <link  rel="stylesheet" href="https://lib.baomitu.com/fancybox/3.5.7/jquery.fancybox.min.css" />



<!-- 主题依赖的图标库，不要自行修改 -->
<!-- Do not modify the link that theme dependent icons -->

<link rel="stylesheet" href="//at.alicdn.com/t/font_1749284_hj8rtnfg7um.css">



<link rel="stylesheet" href="//at.alicdn.com/t/font_1736178_lbnruvf0jn.css">


<link  rel="stylesheet" href="/my-blog/css/main.css" />


  <link id="highlight-css" rel="stylesheet" href="/my-blog/css/highlight.css" />
  
    <link id="highlight-css-dark" rel="stylesheet" href="/my-blog/css/highlight-dark.css" />
  




  <script id="fluid-configs">
    var Fluid = window.Fluid || {};
    Fluid.ctx = Object.assign({}, Fluid.ctx)
    var CONFIG = {"hostname":"walker_kwu.gitee.io","root":"/my-blog/","version":"1.9.2","typing":{"enable":true,"typeSpeed":70,"cursorChar":"_","loop":false,"scope":[]},"anchorjs":{"enable":true,"element":"h1,h2,h3,h4,h5,h6","placement":"left","visible":"hover","icon":""},"progressbar":{"enable":true,"height_px":3,"color":"#29d","options":{"showSpinner":false,"trickleSpeed":100}},"code_language":{"enable":true,"default":"TEXT"},"copy_btn":true,"image_caption":{"enable":true},"image_zoom":{"enable":true,"img_url_replace":["",""]},"toc":{"enable":true,"placement":"left","headingSelector":"h1,h2,h3,h4,h5,h6","collapseDepth":0},"lazyload":{"enable":true,"loading_img":"/img/loading.gif","onlypost":false,"offset_factor":2},"web_analytics":{"enable":false,"follow_dnt":true,"baidu":null,"google":null,"gtag":null,"tencent":{"sid":null,"cid":null},"woyaola":null,"cnzz":null,"leancloud":{"app_id":null,"app_key":null,"server_url":null,"path":"window.location.pathname","ignore_local":false}},"search_path":"/my-blog/local-search.xml"};

    if (CONFIG.web_analytics.follow_dnt) {
      var dntVal = navigator.doNotTrack || window.doNotTrack || navigator.msDoNotTrack;
      Fluid.ctx.dnt = dntVal && (dntVal.startsWith('1') || dntVal.startsWith('yes') || dntVal.startsWith('on'));
    }
  </script>
  <script  src="/my-blog/js/utils.js" ></script>
  <script  src="/my-blog/js/color-schema.js" ></script>
  


  
<meta name="generator" content="Hexo 6.2.0"></head>


<body>
  

  <header>
    

<div class="header-inner" style="height: 70vh;">
  <nav id="navbar" class="navbar fixed-top  navbar-expand-lg navbar-dark scrolling-navbar">
  <div class="container">
    <a class="navbar-brand" href="/my-blog/">
      <strong>木舟</strong>
    </a>

    <button id="navbar-toggler-btn" class="navbar-toggler" type="button" data-toggle="collapse"
            data-target="#navbarSupportedContent"
            aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
      <div class="animated-icon"><span></span><span></span><span></span></div>
    </button>

    <!-- Collapsible content -->
    <div class="collapse navbar-collapse" id="navbarSupportedContent">
      <ul class="navbar-nav ml-auto text-center">
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/my-blog/">
                <i class="iconfont icon-home-fill"></i>
                首页
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/my-blog/archives/">
                <i class="iconfont icon-archive-fill"></i>
                归档
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/my-blog/categories/">
                <i class="iconfont icon-category-fill"></i>
                分类
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/my-blog/tags/">
                <i class="iconfont icon-tags-fill"></i>
                标签
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/my-blog/about/">
                <i class="iconfont icon-user-fill"></i>
                关于
              </a>
            </li>
          
        
        
          <li class="nav-item" id="search-btn">
            <a class="nav-link" target="_self" href="javascript:;" data-toggle="modal" data-target="#modalSearch" aria-label="Search">
              &nbsp;<i class="iconfont icon-search"></i>&nbsp;
            </a>
          </li>
          
        
        
          <li class="nav-item" id="color-toggle-btn">
            <a class="nav-link" target="_self" href="javascript:;" aria-label="Color Toggle">&nbsp;<i
                class="iconfont icon-dark" id="color-toggle-icon"></i>&nbsp;</a>
          </li>
        
      </ul>
    </div>
  </div>
</nav>

  

<div id="banner" class="banner" parallax=true
     style="background: url('/my-blog/img/default.png') no-repeat center center; background-size: cover;">
  <div class="full-bg-img">
    <div class="mask flex-center" style="background-color: rgba(0, 0, 0, 0.3)">
      <div class="banner-text text-center fade-in-up">
        <div class="h2">
          
            <span id="subtitle" data-typed-text="JavaScript Dom操作"></span>
          
        </div>

        
          
  <div class="mt-3">
    
      <span class="post-meta mr-2">
        <i class="iconfont icon-author" aria-hidden="true"></i>
        木舟
      </span>
    
    
      <span class="post-meta">
        <i class="iconfont icon-date-fill" aria-hidden="true"></i>
        <time datetime="2022-10-29 16:55" pubdate>
          2022年10月29日 下午
        </time>
      </span>
    
  </div>

  <div class="mt-1">
    
      <span class="post-meta mr-2">
        <i class="iconfont icon-chart"></i>
        
          7.5k 字
        
      </span>
    

    

    
    
      
        <span id="busuanzi_container_page_pv" style="display: none">
          <i class="iconfont icon-eye" aria-hidden="true"></i>
          <span id="busuanzi_value_page_pv"></span> 次
        </span>
        
      
    
  </div>


        
      </div>

      
    </div>
  </div>
</div>

</div>

  </header>

  <main>
    
      

<div class="container-fluid nopadding-x">
  <div class="row nomargin-x">
    <div class="side-col d-none d-lg-block col-lg-2">
      
  <aside class="sidebar" style="padding-left: 2rem; margin-right: -1rem">
    <div id="toc">
  <p class="toc-header"><i class="iconfont icon-list"></i>&nbsp;目录</p>
  <div class="toc-body" id="toc-body"></div>
</div>



  </aside>


    </div>

    <div class="col-lg-8 nopadding-x-md">
      <div class="container nopadding-x-md" id="board-ctn">
        <div id="board">
          <article class="post-content mx-auto">
            <!-- SEO header -->
            <h1 style="display: none">JavaScript Dom操作</h1>
            
              <p class="note note-info">
                
                  
                    本文最后更新于：2023年5月23日 晚上
                  
                
              </p>
            
            
              <div class="markdown-body">
                
                <h1 id="前言"><a href="#前言" class="headerlink" title="前言"></a>前言</h1><p>Javascript存在的最大意义，就是用来控制页面行为。</p>
<p>通过编写JavaScript代码，来实现页面的交互、数据的展示。</p>
<p>在如今三大前端框架的出现，通过JavaScript直接操作Dom的场景变得越来越少，目前在实际开发中的场景一般为：</p>
<ul>
<li>echarts图表渲染</li>
<li>创建a标签进行资源下载</li>
<li>自适应高度</li>
<li>事件监听</li>
</ul>
<p>而Dom并不是JavaScript语言本身的东西，而是<strong>浏览器</strong>提供给JavaScript能够操作页面的Api。</p>
<p>为什么强调是<strong>浏览器</strong>提供的，因为不同的浏览器可能不兼容一些Dom操作。</p>
<p>有了这些Api，Javascript就能发挥超强的作用，来决定页面应该显示什么。</p>
<p>什么是Dom呢？Dom就是Document，就是整个网页，网页呈现的方式称为 <strong>Dom树</strong></p>
<p>每个标签都相当于Dom树的一个节点，我们主要就是通过Dom Api来操作这些节点。</p>
<h1 id="节点选择"><a href="#节点选择" class="headerlink" title="节点选择"></a>节点选择</h1><p>Dom Api为了方便JavaScript能够准确的定位节点，提供了很多选择元素的Api（只说常用）：</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><code class="hljs javascript"><span class="hljs-keyword">const</span> dom = <span class="hljs-variable language_">document</span><br><br>dom.<span class="hljs-title function_">getElementById</span>(<span class="hljs-string">&#x27;myId&#x27;</span>)  <span class="hljs-comment">// 通过id获取元素，返回一个元素对象</span><br>dom.<span class="hljs-title function_">getElementsByClassName</span>(<span class="hljs-string">&#x27;myClass&#x27;</span>)  <span class="hljs-comment">// 通过class来获取元素，返回一个数组，数组每个元素都是匹配的元素对象</span><br>dom.<span class="hljs-title function_">getElementsByTagName</span>(<span class="hljs-string">&#x27;div&#x27;</span>)  <span class="hljs-comment">// 通过标签来获取元素，返回一个数组，数组每个元素都是匹配的元素对象</span><br>dom.<span class="hljs-title function_">querySelector</span>(<span class="hljs-string">&#x27;#myIdCss&#x27;</span>)  <span class="hljs-comment">// 通过css选择器来获取元素，返回一个元素对象</span><br>dom.<span class="hljs-title function_">querySelectorAll</span>(<span class="hljs-string">&#x27;.myClass1 .myClass2&#x27;</span>) <span class="hljs-comment">// 通过css选择器来获取元素，返回一个数组，数组每个元素都是匹配的元素对象</span><br>dom.<span class="hljs-title function_">getElementById</span>().<span class="hljs-title function_">getElementsByClassName</span>()  <span class="hljs-comment">// 支持链式查找</span><br></code></pre></td></tr></table></figure>


<h1 id="常用元素方法"><a href="#常用元素方法" class="headerlink" title="常用元素方法"></a>常用元素方法</h1><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td><td class="code"><pre><code class="hljs javascript"><span class="hljs-keyword">const</span> span = <span class="hljs-variable language_">document</span>.<span class="hljs-title function_">createElement</span>(<span class="hljs-string">&#x27;span&#x27;</span>)<br><span class="hljs-keyword">const</span> el = <span class="hljs-variable language_">document</span>.<span class="hljs-title function_">getElementById</span>(<span class="hljs-string">&#x27;test&#x27;</span>)<br><br>span.<span class="hljs-property">innerText</span> = <span class="hljs-string">&#x27;testSpan&#x27;</span>  <span class="hljs-comment">// 修改元素的文本</span><br><span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(span.<span class="hljs-property">innerText</span>);  <span class="hljs-comment">// 获取元素文本</span><br><br>el.<span class="hljs-title function_">appendChild</span>(span) <span class="hljs-comment">// 往元素中添加元素</span><br>el.<span class="hljs-title function_">removeChild</span>(span) <span class="hljs-comment">// 从元素中删除元素</span><br>el.<span class="hljs-property">classList</span>  <span class="hljs-comment">// 元素所有的class，可以进行增减</span><br>el.<span class="hljs-title function_">setAttribute</span>(<span class="hljs-string">&#x27;class&#x27;</span>, <span class="hljs-string">&#x27;test2&#x27;</span>) <span class="hljs-comment">// 设置元素的属性值，全覆盖</span><br>el.<span class="hljs-title function_">removeAttribute</span>(<span class="hljs-string">&#x27;class1&#x27;</span>) <span class="hljs-comment">// 删除元素属性值</span><br><span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(el.<span class="hljs-property">style</span>.<span class="hljs-property">backgroundColor</span>);  <span class="hljs-comment">// 获取元素行内样式</span><br>el.<span class="hljs-property">style</span>.<span class="hljs-property">marginLeft</span> = <span class="hljs-string">&#x27;10px&#x27;</span> <span class="hljs-comment">// 设置元素行内样式</span><br><br><span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(el.<span class="hljs-property">offsetHeight</span>); <span class="hljs-comment">// 获取元素占地高度，包括边框</span><br><span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(el.<span class="hljs-property">offsetWidth</span>);  <span class="hljs-comment">// 获取元素占地宽度，包括边框</span><br><br><span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(el.<span class="hljs-property">scrollHeight</span>); <span class="hljs-comment">// 获取元素本身高度，不包括边框</span><br><span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(el.<span class="hljs-property">scrollWidth</span>); <span class="hljs-comment">// 获取元素本身宽度，不包括边框</span><br></code></pre></td></tr></table></figure>

<p>因为后续会学习Vue，所以其他不算常用的方法就不多列举了。</p>
<p>详细的方法可以参考 <strong>菜鸟教程</strong></p>
<blockquote>
<p>Dom对象方法：<a target="_blank" rel="noopener" href="https://www.runoob.com/jsref/dom-obj-document.html">https://www.runoob.com/jsref/dom-obj-document.html</a></p>
</blockquote>
<blockquote>
<p>Dom元素方法: <a target="_blank" rel="noopener" href="https://www.runoob.com/jsref/dom-obj-all.html">https://www.runoob.com/jsref/dom-obj-all.html</a></p>
</blockquote>
<blockquote>
<p>Dom事件方法: <a target="_blank" rel="noopener" href="https://www.runoob.com/jsref/dom-obj-event.html">https://www.runoob.com/jsref/dom-obj-event.html</a></p>
</blockquote>
<h1 id="常见的应用场景"><a href="#常见的应用场景" class="headerlink" title="常见的应用场景"></a>常见的应用场景</h1><h2 id="axios下载文件"><a href="#axios下载文件" class="headerlink" title="axios下载文件"></a>axios下载文件</h2><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><code class="hljs javascript"><span class="hljs-comment">// 由于axios还没学习，所以只编写下载文件的代码</span><br><br><span class="hljs-keyword">const</span> <span class="hljs-title function_">downloadFile</span> = (<span class="hljs-params">fileData, fileName</span>)=&gt;&#123;<br>    <span class="hljs-keyword">const</span> blob = <span class="hljs-keyword">new</span> <span class="hljs-title class_">Blob</span>([fileData])<br>    <span class="hljs-keyword">const</span> url = <span class="hljs-variable language_">window</span>.<span class="hljs-property">URL</span>.<span class="hljs-title function_">createObjectURL</span>(blob)<br>    <span class="hljs-keyword">const</span> el = <span class="hljs-variable language_">document</span>.<span class="hljs-title function_">createElement</span>(<span class="hljs-string">&#x27;a&#x27;</span>);<br>    el.<span class="hljs-property">href</span> = url;<br>    el.<span class="hljs-title function_">click</span>();<br>    el.<span class="hljs-property">download</span> = fileName<br>    <span class="hljs-variable language_">document</span>.<span class="hljs-title function_">appendChild</span>(el)<br>    el.<span class="hljs-title function_">click</span>()<br>    <span class="hljs-variable language_">document</span>.<span class="hljs-title function_">removeChild</span>(el)<br>&#125;<br></code></pre></td></tr></table></figure>

<h2 id="监听页面高度，修改页面元素高度"><a href="#监听页面高度，修改页面元素高度" class="headerlink" title="监听页面高度，修改页面元素高度"></a>监听页面高度，修改页面元素高度</h2><p>下面通过Vue来进行举例</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><code class="hljs vue">const height = ref(0)<br><br>const getHeight = ()=&gt;&#123;<br>    const el = document.getElementById(&#x27;myId&#x27;)<br>    height.value = el.offsetHeight - 100<br>&#125;<br><br>onMounted(() =&gt; &#123;<br>    getHeight()  // 先初始化高度<br>    window.addEventListener(&#x27;resize&#x27;, getHeight)  // 监听页面尺寸变化<br>&#125;)<br><br>onBeforeUnmount(() =&gt; &#123;<br>    window.removeEventListener(&#x27;resize&#x27;, getHeight)  // 移除事件监听<br>&#125;)<br></code></pre></td></tr></table></figure>


<h2 id="设置元素行为"><a href="#设置元素行为" class="headerlink" title="设置元素行为"></a>设置元素行为</h2><figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br></pre></td><td class="code"><pre><code class="hljs html"><span class="hljs-meta">&lt;!DOCTYPE <span class="hljs-keyword">html</span>&gt;</span><br><span class="hljs-tag">&lt;<span class="hljs-name">html</span> <span class="hljs-attr">lang</span>=<span class="hljs-string">&quot;en&quot;</span>&gt;</span><br><span class="hljs-tag">&lt;<span class="hljs-name">head</span>&gt;</span><br>    <span class="hljs-tag">&lt;<span class="hljs-name">meta</span> <span class="hljs-attr">charset</span>=<span class="hljs-string">&quot;UTF-8&quot;</span>&gt;</span><br>    <span class="hljs-tag">&lt;<span class="hljs-name">meta</span> <span class="hljs-attr">http-equiv</span>=<span class="hljs-string">&quot;X-UA-Compatible&quot;</span> <span class="hljs-attr">content</span>=<span class="hljs-string">&quot;IE=edge&quot;</span>&gt;</span><br>    <span class="hljs-tag">&lt;<span class="hljs-name">meta</span> <span class="hljs-attr">name</span>=<span class="hljs-string">&quot;viewport&quot;</span> <span class="hljs-attr">content</span>=<span class="hljs-string">&quot;width=device-width, initial-scale=1.0&quot;</span>&gt;</span><br>    <span class="hljs-tag">&lt;<span class="hljs-name">title</span>&gt;</span>Document<span class="hljs-tag">&lt;/<span class="hljs-name">title</span>&gt;</span><br><span class="hljs-tag">&lt;/<span class="hljs-name">head</span>&gt;</span><br><span class="hljs-tag">&lt;<span class="hljs-name">body</span>&gt;</span><br>    <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">id</span>=<span class="hljs-string">&quot;container&quot;</span>&gt;</span><br>    <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span><br><span class="hljs-tag">&lt;/<span class="hljs-name">body</span>&gt;</span><br><br><span class="hljs-tag">&lt;<span class="hljs-name">script</span>&gt;</span><span class="language-javascript"></span><br><span class="language-javascript"></span><br><span class="language-javascript"><span class="hljs-keyword">const</span> el = <span class="hljs-variable language_">document</span>.<span class="hljs-title function_">querySelector</span>(<span class="hljs-string">&#x27;#container&#x27;</span>)</span><br><span class="language-javascript"></span><br><span class="language-javascript">el.<span class="hljs-property">style</span>.<span class="hljs-property">width</span> = <span class="hljs-string">&quot;100px&quot;</span></span><br><span class="language-javascript">el.<span class="hljs-property">style</span>.<span class="hljs-property">height</span> = <span class="hljs-string">&quot;100px&quot;</span></span><br><span class="language-javascript">el.<span class="hljs-property">style</span>.<span class="hljs-property">backgroundColor</span> = <span class="hljs-string">&#x27;red&#x27;</span></span><br><span class="language-javascript">el.<span class="hljs-property">style</span>.<span class="hljs-property">position</span> = <span class="hljs-string">&#x27;absolute&#x27;</span></span><br><span class="language-javascript">el.<span class="hljs-property">style</span>.<span class="hljs-property">top</span> = <span class="hljs-number">0</span></span><br><span class="language-javascript">el.<span class="hljs-property">style</span>.<span class="hljs-property">left</span> = <span class="hljs-number">0</span></span><br><span class="language-javascript"></span><br><span class="language-javascript"><span class="hljs-comment">// 监听键盘事件</span></span><br><span class="language-javascript"><span class="hljs-variable language_">document</span>.<span class="hljs-title function_">addEventListener</span>(<span class="hljs-string">&#x27;keydown&#x27;</span>, <span class="hljs-function">(<span class="hljs-params">e</span>)=&gt;</span>&#123;</span><br><span class="language-javascript">    <span class="hljs-keyword">switch</span>(e.<span class="hljs-property">which</span>)&#123;</span><br><span class="language-javascript">        <span class="hljs-keyword">case</span> <span class="hljs-number">37</span>: <span class="hljs-comment">// 左</span></span><br><span class="language-javascript">            el.<span class="hljs-property">style</span>.<span class="hljs-property">left</span> = <span class="hljs-built_in">parseInt</span>(el.<span class="hljs-property">style</span>.<span class="hljs-property">left</span>) - <span class="hljs-number">10</span> + <span class="hljs-string">&#x27;px&#x27;</span></span><br><span class="language-javascript">            <span class="hljs-keyword">break</span></span><br><span class="language-javascript">        <span class="hljs-keyword">case</span> <span class="hljs-number">38</span>: <span class="hljs-comment">// 上</span></span><br><span class="language-javascript">            el.<span class="hljs-property">style</span>.<span class="hljs-property">top</span> = <span class="hljs-built_in">parseInt</span>(el.<span class="hljs-property">style</span>.<span class="hljs-property">top</span>) - <span class="hljs-number">10</span> + <span class="hljs-string">&#x27;px&#x27;</span></span><br><span class="language-javascript">            <span class="hljs-keyword">break</span></span><br><span class="language-javascript">        <span class="hljs-keyword">case</span> <span class="hljs-number">39</span>: <span class="hljs-comment">// 右</span></span><br><span class="language-javascript">            el.<span class="hljs-property">style</span>.<span class="hljs-property">left</span> = <span class="hljs-built_in">parseInt</span>(el.<span class="hljs-property">style</span>.<span class="hljs-property">left</span>) + <span class="hljs-number">10</span> + <span class="hljs-string">&#x27;px&#x27;</span></span><br><span class="language-javascript">            <span class="hljs-keyword">break</span></span><br><span class="language-javascript">        <span class="hljs-keyword">case</span> <span class="hljs-number">40</span>: <span class="hljs-comment">// 下</span></span><br><span class="language-javascript">            el.<span class="hljs-property">style</span>.<span class="hljs-property">top</span> = <span class="hljs-built_in">parseInt</span>(el.<span class="hljs-property">style</span>.<span class="hljs-property">top</span>) + <span class="hljs-number">10</span> + <span class="hljs-string">&#x27;px&#x27;</span></span><br><span class="language-javascript">            <span class="hljs-keyword">break</span></span><br><span class="language-javascript">    &#125;</span><br><span class="language-javascript">&#125;)</span><br><span class="language-javascript"></span><br><span class="language-javascript"></span><br><span class="language-javascript"></span><span class="hljs-tag">&lt;/<span class="hljs-name">script</span>&gt;</span><br><span class="hljs-tag">&lt;/<span class="hljs-name">html</span>&gt;</span><br></code></pre></td></tr></table></figure>

<h2 id="echarts"><a href="#echarts" class="headerlink" title="echarts"></a>echarts</h2><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><code class="hljs javascript"><span class="hljs-comment">// 伪代码</span><br><span class="hljs-keyword">import</span> echarts <span class="hljs-keyword">from</span> <span class="hljs-string">&#x27;echarts.js&#x27;</span><br><span class="hljs-keyword">let</span> myEchartsOptions<br><br><span class="hljs-keyword">const</span> el = <span class="hljs-variable language_">document</span>.<span class="hljs-title function_">querySelector</span>(<span class="hljs-string">&#x27;#myCharts&#x27;</span>)<br><br>echart.<span class="hljs-title function_">init</span>(el)<br>echart.<span class="hljs-title function_">setOption</span>(myEchartsOptions)<br><br></code></pre></td></tr></table></figure>


<h2 id="切换图片"><a href="#切换图片" class="headerlink" title="切换图片"></a>切换图片</h2><figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br></pre></td><td class="code"><pre><code class="hljs html"><span class="hljs-meta">&lt;!DOCTYPE <span class="hljs-keyword">html</span>&gt;</span><br><span class="hljs-tag">&lt;<span class="hljs-name">html</span> <span class="hljs-attr">lang</span>=<span class="hljs-string">&quot;en&quot;</span>&gt;</span><br><span class="hljs-tag">&lt;<span class="hljs-name">head</span>&gt;</span><br>    <span class="hljs-tag">&lt;<span class="hljs-name">meta</span> <span class="hljs-attr">charset</span>=<span class="hljs-string">&quot;UTF-8&quot;</span>&gt;</span><br>    <span class="hljs-tag">&lt;<span class="hljs-name">title</span>&gt;</span>Title<span class="hljs-tag">&lt;/<span class="hljs-name">title</span>&gt;</span><br><span class="hljs-tag">&lt;/<span class="hljs-name">head</span>&gt;</span><br><span class="hljs-tag">&lt;<span class="hljs-name">body</span>&gt;</span><br>    <span class="hljs-tag">&lt;<span class="hljs-name">button</span> <span class="hljs-attr">class</span>=<span class="hljs-string">&quot;change&quot;</span>&gt;</span>切换<span class="hljs-tag">&lt;/<span class="hljs-name">button</span>&gt;</span><br>    <span class="hljs-tag">&lt;<span class="hljs-name">br</span>&gt;</span><br>    <span class="hljs-tag">&lt;<span class="hljs-name">img</span> <span class="hljs-attr">class</span>=<span class="hljs-string">&quot;box&quot;</span> <span class="hljs-attr">src</span>=<span class="hljs-string">&quot;&quot;</span> <span class="hljs-attr">alt</span>=<span class="hljs-string">&quot;&quot;</span>&gt;</span><br>    <span class="hljs-tag">&lt;<span class="hljs-name">script</span>&gt;</span><span class="language-javascript"></span><br><span class="language-javascript">    <span class="hljs-comment">// 最基本的图片切换</span></span><br><span class="language-javascript">    <span class="hljs-keyword">var</span> images_list = [<span class="hljs-string">&quot;assets/images/banner-1.jpg&quot;</span>, <span class="hljs-string">&quot;assets/images/banner-2.jpg&quot;</span>, <span class="hljs-string">&quot;assets/images/banner-3.jpg&quot;</span>]</span><br><span class="language-javascript">    <span class="hljs-keyword">var</span> box = <span class="hljs-variable language_">document</span>.<span class="hljs-title function_">querySelector</span>(<span class="hljs-string">&quot;.box&quot;</span>)</span><br><span class="language-javascript">    <span class="hljs-keyword">var</span> change = <span class="hljs-variable language_">document</span>.<span class="hljs-title function_">querySelector</span>(<span class="hljs-string">&quot;.change&quot;</span>)</span><br><span class="language-javascript">    <span class="hljs-keyword">var</span> currentIndex = <span class="hljs-number">0</span>;</span><br><span class="language-javascript">    box.<span class="hljs-property">src</span>=images_list[currentIndex];</span><br><span class="language-javascript"></span><br><span class="language-javascript">    <span class="hljs-keyword">function</span> <span class="hljs-title function_">start</span>(<span class="hljs-params"></span>)&#123;</span><br><span class="language-javascript">        currentIndex++;</span><br><span class="language-javascript">        <span class="hljs-keyword">if</span>(currentIndex&gt;=images_list.<span class="hljs-property">length</span>)&#123;</span><br><span class="language-javascript">            currentIndex = <span class="hljs-number">0</span>;</span><br><span class="language-javascript">        &#125;</span><br><span class="language-javascript">        box.<span class="hljs-property">src</span>=images_list[currentIndex];</span><br><span class="language-javascript">    &#125;</span><br><span class="language-javascript"></span><br><span class="language-javascript">    change.<span class="hljs-property">onclick</span> = start;</span><br><span class="language-javascript"></span><br><span class="language-javascript">    </span><span class="hljs-tag">&lt;/<span class="hljs-name">script</span>&gt;</span><br><span class="hljs-tag">&lt;/<span class="hljs-name">body</span>&gt;</span><br><span class="hljs-tag">&lt;/<span class="hljs-name">html</span>&gt;</span><br><br></code></pre></td></tr></table></figure>


<h2 id="跳转到元素区域"><a href="#跳转到元素区域" class="headerlink" title="跳转到元素区域"></a>跳转到元素区域</h2><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><code class="hljs javascript"><span class="hljs-keyword">const</span> el<br>el.<span class="hljs-title function_">scrollIntoView</span>(<span class="hljs-literal">true</span>);<br></code></pre></td></tr></table></figure>


<h1 id="事件绑定"><a href="#事件绑定" class="headerlink" title="事件绑定"></a>事件绑定</h1><p>所谓的事件（Event）,其实就是用户与浏览器之间的一次交互过程或交互行为。例如：用户输入一段内容，用户鼠标晃动一下等等。<br>js中提供了大量的事件操作给开发者, 在用户发生事件操作以后，进行反馈，响应。<br>事件类型有很多，但是操作基本都是一样的，只是里面的代码因为事件的不同，而执行的方式，属性不同而已。</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br></pre></td><td class="code"><pre><code class="hljs js"><span class="hljs-comment">/**</span><br><span class="hljs-comment">    * 事件对象</span><br><span class="hljs-comment">    * element.onmousemove	鼠标被移动。</span><br><span class="hljs-comment">    * onmousedown	        鼠标按钮被按下。</span><br><span class="hljs-comment">    * onmouseup	        鼠标按键被松开。</span><br><span class="hljs-comment">    * element.onclick	    当用户点击某个对象时调用的事件句柄。</span><br><span class="hljs-comment">    * element.ondblclick	当用户双击某个对象时调用的事件句柄。</span><br><span class="hljs-comment">    * element.onscroll     在元素滚动条开始滚动的时候触发</span><br><span class="hljs-comment">    *</span><br><span class="hljs-comment">    *</span><br><span class="hljs-comment">    * onabort	    图像的加载被中断。</span><br><span class="hljs-comment">    * onblur	    元素失去焦点。</span><br><span class="hljs-comment">    * onchange	    域的内容被改变。</span><br><span class="hljs-comment">    * onerror	    在加载文档或图像时发生错误。</span><br><span class="hljs-comment">    * onfocus	    元素获得焦点。</span><br><span class="hljs-comment">    * onkeydown	某个键盘按键被按下。</span><br><span class="hljs-comment">    * onkeypress	某个键盘按键被按下并松开。</span><br><span class="hljs-comment">    * onkeyup	    某个键盘按键被松开。</span><br><span class="hljs-comment">    * onload	    一张页面或一幅图像完成加载。</span><br><span class="hljs-comment">    *</span><br><span class="hljs-comment">    * onmouseout	鼠标从某元素移开。</span><br><span class="hljs-comment">    * onmouseover	鼠标移到某元素之上。</span><br><span class="hljs-comment">    *</span><br><span class="hljs-comment">    * onreset	    重置按钮被点击。</span><br><span class="hljs-comment">    * onresize	    窗口或框架被重新调整大小。</span><br><span class="hljs-comment">    * onselect	    文本被选中。</span><br><span class="hljs-comment">    * onsubmit	    确认按钮被点击。</span><br><span class="hljs-comment">    * onunload	    用户退出页面。</span><br><span class="hljs-comment">    * </span><br><span class="hljs-comment">    * 事件属性</span><br><span class="hljs-comment">    * event.clientX	    返回当事件被触发时，当前窗口中鼠标指针的水平坐标。</span><br><span class="hljs-comment">    * event.clientY	    返回当事件被触发时，当前窗口中鼠标指针的垂直坐标。</span><br><span class="hljs-comment">    * event.pageX	        返回当事件被触发时，当前页面中鼠标指针的水平坐标。</span><br><span class="hljs-comment">    * event.pageY	        返回当事件被触发时，当前页面中鼠标指针的垂直坐标。</span><br><span class="hljs-comment">    */</span><br></code></pre></td></tr></table></figure>

<h2 id="常用的鼠标事件"><a href="#常用的鼠标事件" class="headerlink" title="常用的鼠标事件"></a>常用的鼠标事件</h2><ul>
<li>click, 点击</li>
<li>mouseenter， 鼠标移入</li>
<li>mouseleave，鼠标移出</li>
</ul>
<h2 id="常用的表单事件"><a href="#常用的表单事件" class="headerlink" title="常用的表单事件"></a>常用的表单事件</h2><ul>
<li>change，值改变</li>
<li>focus，聚焦</li>
<li>blur，失焦</li>
</ul>
<h2 id="键盘事件"><a href="#键盘事件" class="headerlink" title="键盘事件"></a>键盘事件</h2><ul>
<li>keydown，键盘按下</li>
<li>keyup，键盘松开</li>
</ul>
<h2 id="如何绑定"><a href="#如何绑定" class="headerlink" title="如何绑定"></a>如何绑定</h2><p>在js中使用事件时,必须事件名称左边加上on<br>例如:给一个元素绑定点击事件则是 onclick…</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><code class="hljs javascript"><span class="hljs-keyword">const</span> el = <span class="hljs-variable language_">document</span>.<span class="hljs-title function_">getElementById</span>(<span class="hljs-string">&#x27;box&#x27;</span>)<br><br>el.<span class="hljs-property">onclick</span> = <span class="hljs-function">()=&gt;</span>&#123;<br>    <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">&quot;点击事件&quot;</span>);<br>&#125;<br><br>el.<span class="hljs-property">onmouseenter</span> = <span class="hljs-function">()=&gt;</span>&#123;<br>    <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">&quot;鼠标移入&quot;</span>);<br>&#125;<br></code></pre></td></tr></table></figure>


<h2 id="事件冒泡"><a href="#事件冒泡" class="headerlink" title="事件冒泡"></a>事件冒泡</h2><p>在事件触发时，事件的影响出内往外进行逐层传递，这个过程就是事件冒泡（Event Propagation）.</p>
<p>比如我点击 【标签内容】 时，会触发body、div、p元素的事件。</p>
<p>好处：如果我想要某个元素有点击事件，就直接绑定某个元素就好了，不需要考虑他底下有多少嵌套子元素<br>坏处：有时候父元素与子元素绑定同类型事件时，父元素不希望被触发的事件，会因为子元素被触发了而直接执行。</p>
<figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br></pre></td><td class="code"><pre><code class="hljs html"><span class="hljs-meta">&lt;!DOCTYPE <span class="hljs-keyword">html</span>&gt;</span><br><span class="hljs-tag">&lt;<span class="hljs-name">html</span> <span class="hljs-attr">lang</span>=<span class="hljs-string">&quot;en&quot;</span>&gt;</span><br><span class="hljs-tag">&lt;<span class="hljs-name">head</span>&gt;</span><br>    <span class="hljs-tag">&lt;<span class="hljs-name">meta</span> <span class="hljs-attr">charset</span>=<span class="hljs-string">&quot;UTF-8&quot;</span>&gt;</span><br>    <span class="hljs-tag">&lt;<span class="hljs-name">title</span>&gt;</span>Title<span class="hljs-tag">&lt;/<span class="hljs-name">title</span>&gt;</span><br><span class="hljs-tag">&lt;/<span class="hljs-name">head</span>&gt;</span><br><span class="hljs-tag">&lt;<span class="hljs-name">body</span> <span class="hljs-attr">onclick</span>=<span class="hljs-string">&quot;show(this)&quot;</span>&gt;</span><br>    <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">&quot;box1&quot;</span>  <span class="hljs-attr">onclick</span>=<span class="hljs-string">&quot;show(this)&quot;</span>&gt;</span><br>        <span class="hljs-tag">&lt;<span class="hljs-name">h1</span>&gt;</span>标题<span class="hljs-tag">&lt;/<span class="hljs-name">h1</span>&gt;</span><br>        <span class="hljs-tag">&lt;<span class="hljs-name">p</span> <span class="hljs-attr">class</span>=<span class="hljs-string">&quot;p1&quot;</span> <span class="hljs-attr">onclick</span>=<span class="hljs-string">&quot;show(this)&quot;</span>&gt;</span><br>            <span class="hljs-tag">&lt;<span class="hljs-name">span</span>&gt;</span>文章内容<span class="hljs-tag">&lt;/<span class="hljs-name">span</span>&gt;</span><span class="hljs-tag">&lt;<span class="hljs-name">br</span>&gt;</span><br>            <span class="hljs-tag">&lt;<span class="hljs-name">span</span> <span class="hljs-attr">href</span>=<span class="hljs-string">&quot;&quot;</span> <span class="hljs-attr">class</span>=<span class="hljs-string">&quot;a1&quot;</span> <span class="hljs-attr">onclick</span>=<span class="hljs-string">&quot;show(this)&quot;</span>&gt;</span>标签内容<span class="hljs-tag">&lt;/<span class="hljs-name">span</span>&gt;</span><br>        <span class="hljs-tag">&lt;/<span class="hljs-name">p</span>&gt;</span><br>    <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span><br>    <span class="hljs-tag">&lt;<span class="hljs-name">script</span>&gt;</span><span class="language-javascript"></span><br><span class="language-javascript">    <span class="hljs-keyword">function</span> <span class="hljs-title function_">show</span>(<span class="hljs-params">ele</span>)&#123;</span><br><span class="language-javascript">        <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(ele);</span><br><span class="language-javascript">        <span class="hljs-keyword">return</span> <span class="hljs-literal">false</span>; <span class="hljs-comment">// 阻止页面跳转或表单提交</span></span><br><span class="language-javascript">    &#125;</span><br><span class="language-javascript">    </span><span class="hljs-tag">&lt;/<span class="hljs-name">script</span>&gt;</span><br><span class="hljs-tag">&lt;/<span class="hljs-name">body</span>&gt;</span><br><span class="hljs-tag">&lt;/<span class="hljs-name">html</span>&gt;</span><br></code></pre></td></tr></table></figure>


<h2 id="阻止事件冒泡"><a href="#阻止事件冒泡" class="headerlink" title="阻止事件冒泡"></a>阻止事件冒泡</h2><p><strong>event.stopPropagation()</strong></p>
<figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br></pre></td><td class="code"><pre><code class="hljs html"><span class="hljs-meta">&lt;!DOCTYPE <span class="hljs-keyword">html</span>&gt;</span><br><span class="hljs-tag">&lt;<span class="hljs-name">html</span> <span class="hljs-attr">lang</span>=<span class="hljs-string">&quot;en&quot;</span>&gt;</span><br><span class="hljs-tag">&lt;<span class="hljs-name">head</span>&gt;</span><br>    <span class="hljs-tag">&lt;<span class="hljs-name">meta</span> <span class="hljs-attr">charset</span>=<span class="hljs-string">&quot;UTF-8&quot;</span>&gt;</span><br>    <span class="hljs-tag">&lt;<span class="hljs-name">title</span>&gt;</span>Title<span class="hljs-tag">&lt;/<span class="hljs-name">title</span>&gt;</span><br><span class="hljs-tag">&lt;/<span class="hljs-name">head</span>&gt;</span><br><span class="hljs-tag">&lt;<span class="hljs-name">body</span> <span class="hljs-attr">onclick</span>=<span class="hljs-string">&quot;show(this)&quot;</span>&gt;</span><br>    <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">&quot;box1&quot;</span>  <span class="hljs-attr">onclick</span>=<span class="hljs-string">&quot;show(this)&quot;</span>&gt;</span><br>        <span class="hljs-tag">&lt;<span class="hljs-name">h1</span>&gt;</span>标题<span class="hljs-tag">&lt;/<span class="hljs-name">h1</span>&gt;</span><br>        <span class="hljs-tag">&lt;<span class="hljs-name">p</span> <span class="hljs-attr">class</span>=<span class="hljs-string">&quot;p1&quot;</span> <span class="hljs-attr">onclick</span>=<span class="hljs-string">&quot;show(this)&quot;</span>&gt;</span><br>            <span class="hljs-tag">&lt;<span class="hljs-name">span</span>&gt;</span>文章内容<span class="hljs-tag">&lt;/<span class="hljs-name">span</span>&gt;</span><span class="hljs-tag">&lt;<span class="hljs-name">br</span>&gt;</span><br>            <span class="hljs-tag">&lt;<span class="hljs-name">span</span> <span class="hljs-attr">href</span>=<span class="hljs-string">&quot;&quot;</span> <span class="hljs-attr">class</span>=<span class="hljs-string">&quot;a1&quot;</span> <span class="hljs-attr">onclick</span>=<span class="hljs-string">&quot;show(this)&quot;</span>&gt;</span>标签内容<span class="hljs-tag">&lt;/<span class="hljs-name">span</span>&gt;</span><br>        <span class="hljs-tag">&lt;/<span class="hljs-name">p</span>&gt;</span><br>    <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span><br>    <span class="hljs-tag">&lt;<span class="hljs-name">script</span>&gt;</span><span class="language-javascript"></span><br><span class="language-javascript">    <span class="hljs-keyword">function</span> <span class="hljs-title function_">show</span>(<span class="hljs-params">ele</span>)&#123;</span><br><span class="language-javascript">        <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(ele);</span><br><span class="language-javascript">        event.<span class="hljs-title function_">stopPropagation</span>()</span><br><span class="language-javascript">    &#125;</span><br><span class="language-javascript">    </span><span class="hljs-tag">&lt;/<span class="hljs-name">script</span>&gt;</span><br><span class="hljs-tag">&lt;/<span class="hljs-name">body</span>&gt;</span><br><span class="hljs-tag">&lt;/<span class="hljs-name">html</span>&gt;</span><br></code></pre></td></tr></table></figure>

<h2 id="阻止默认事件"><a href="#阻止默认事件" class="headerlink" title="阻止默认事件"></a>阻止默认事件</h2><figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><code class="hljs html"><span class="hljs-comment">&lt;!-- 阻止a标签的跳转事件 --&gt;</span><br><span class="hljs-tag">&lt;<span class="hljs-name">a</span> <span class="hljs-attr">href</span>=<span class="hljs-string">&quot;www.baidu.com&quot;</span> <span class="hljs-attr">onclick</span>=<span class="hljs-string">&quot;event.preventDefault()&quot;</span>&gt;</span>baidu<span class="hljs-tag">&lt;/<span class="hljs-name">a</span>&gt;</span><br></code></pre></td></tr></table></figure>
                
              </div>
            
            <hr/>
            <div>
              <div class="post-metas my-3">
  
    <div class="post-meta mr-3 d-flex align-items-center">
      <i class="iconfont icon-category"></i>
      

<span class="category-chains">
  
  
    
      <span class="category-chain">
        
  <a href="/my-blog/categories/%E6%B5%8B%E8%AF%95%E5%BC%80%E5%8F%91/" class="category-chain-item">测试开发</a>
  
  
    <span>></span>
    
  <a href="/my-blog/categories/%E6%B5%8B%E8%AF%95%E5%BC%80%E5%8F%91/javascript/" class="category-chain-item">javascript</a>
  
  

  

      </span>
    
  
</span>

    </div>
  
  
    <div class="post-meta">
      <i class="iconfont icon-tags"></i>
      
        <a href="/my-blog/tags/%E6%B5%8B%E8%AF%95%E5%BC%80%E5%8F%91/">#测试开发</a>
      
        <a href="/my-blog/tags/%E6%B5%8B%E8%AF%95%E5%B9%B3%E5%8F%B0/">#测试平台</a>
      
        <a href="/my-blog/tags/javascript/">#javascript</a>
      
    </div>
  
</div>


              
  

  <div class="license-box my-3">
    <div class="license-title">
      <div>JavaScript Dom操作</div>
      <div>https://walker_kwu.gitee.io/my-blog/2022/10/29/notes/js/dom/</div>
    </div>
    <div class="license-meta">
      
        <div class="license-meta-item">
          <div>作者</div>
          <div>木舟</div>
        </div>
      
      
        <div class="license-meta-item license-meta-date">
          <div>发布于</div>
          <div>2022年10月29日</div>
        </div>
      
      
        <div class="license-meta-item license-meta-date">
          <div>更新于</div>
          <div>2023年5月23日</div>
        </div>
      
      <div class="license-meta-item">
        <div>许可协议</div>
        <div>
          
            
            
              <a target="_blank" href="https://creativecommons.org/licenses/by-nc-sa/4.0/">
              <span class="hint--top hint--rounded" aria-label="BY - 署名">
                <i class="iconfont icon-by"></i>
              </span>
              </a>
            
              <a target="_blank" href="https://creativecommons.org/licenses/by-nc-sa/4.0/">
              <span class="hint--top hint--rounded" aria-label="NC - 非商业性使用">
                <i class="iconfont icon-nc"></i>
              </span>
              </a>
            
              <a target="_blank" href="https://creativecommons.org/licenses/by-nc-sa/4.0/">
              <span class="hint--top hint--rounded" aria-label="SA - 相同方式共享">
                <i class="iconfont icon-sa"></i>
              </span>
              </a>
            
          
        </div>
      </div>
    </div>
    <div class="license-icon iconfont"></div>
  </div>



              
                <div class="post-prevnext my-3">
                  <article class="post-prev col-6">
                    
                    
                      <a href="/my-blog/2022/10/30/notes/js/bom/" title="JavaScript-Bom操作">
                        <i class="iconfont icon-arrowleft"></i>
                        <span class="hidden-mobile">JavaScript-Bom操作</span>
                        <span class="visible-mobile">上一篇</span>
                      </a>
                    
                  </article>
                  <article class="post-next col-6">
                    
                    
                      <a href="/my-blog/2022/10/23/notes/js/newKnowledge/" title="JavaScript新语法">
                        <span class="hidden-mobile">JavaScript新语法</span>
                        <span class="visible-mobile">下一篇</span>
                        <i class="iconfont icon-arrowright"></i>
                      </a>
                    
                  </article>
                </div>
              
            </div>

            
          </article>
        </div>
      </div>
    </div>

    <div class="side-col d-none d-lg-block col-lg-2">
      

    </div>
  </div>
</div>





  



  



  



  



  







    

    
      <a id="scroll-top-button" aria-label="TOP" href="#" role="button">
        <i class="iconfont icon-arrowup" aria-hidden="true"></i>
      </a>
    

    
      <div class="modal fade" id="modalSearch" tabindex="-1" role="dialog" aria-labelledby="ModalLabel"
     aria-hidden="true">
  <div class="modal-dialog modal-dialog-scrollable modal-lg" role="document">
    <div class="modal-content">
      <div class="modal-header text-center">
        <h4 class="modal-title w-100 font-weight-bold">搜索</h4>
        <button type="button" id="local-search-close" class="close" data-dismiss="modal" aria-label="Close">
          <span aria-hidden="true">&times;</span>
        </button>
      </div>
      <div class="modal-body mx-3">
        <div class="md-form mb-5">
          <input type="text" id="local-search-input" class="form-control validate">
          <label data-error="x" data-success="v" for="local-search-input">关键词</label>
        </div>
        <div class="list-group" id="local-search-result"></div>
      </div>
    </div>
  </div>
</div>

    

    
  </main>

  <footer>
    <div class="footer-inner">
  
    <div class="footer-content">
       <span>By 木舟</span> <i class="iconfont icon-love"></i> 
    </div>
  
  
    <div class="statistics">
  
  

  
    
      <span id="busuanzi_container_site_pv" style="display: none">
        总访问量 
        <span id="busuanzi_value_site_pv"></span>
         次
      </span>
    
    
      <span id="busuanzi_container_site_uv" style="display: none">
        总访客数 
        <span id="busuanzi_value_site_uv"></span>
         人
      </span>
    
    
  
</div>

  
  
  
</div>

  </footer>

  <!-- Scripts -->
  
  <script  src="https://lib.baomitu.com/nprogress/0.2.0/nprogress.min.js" ></script>
  <link  rel="stylesheet" href="https://lib.baomitu.com/nprogress/0.2.0/nprogress.min.css" />

  <script>
    NProgress.configure({"showSpinner":false,"trickleSpeed":100})
    NProgress.start()
    window.addEventListener('load', function() {
      NProgress.done();
    })
  </script>


<script  src="https://lib.baomitu.com/jquery/3.6.0/jquery.min.js" ></script>
<script  src="https://lib.baomitu.com/twitter-bootstrap/4.6.1/js/bootstrap.min.js" ></script>
<script  src="/my-blog/js/events.js" ></script>
<script  src="/my-blog/js/plugins.js" ></script>


  <script  src="https://lib.baomitu.com/typed.js/2.0.12/typed.min.js" ></script>
  <script>
    (function (window, document) {
      var typing = Fluid.plugins.typing;
      var subtitle = document.getElementById('subtitle');
      if (!subtitle || !typing) {
        return;
      }
      var text = subtitle.getAttribute('data-typed-text');
      
        typing(text);
      
    })(window, document);
  </script>




  
    <script  src="/my-blog/js/img-lazyload.js" ></script>
  




  
<script>
  Fluid.utils.createScript('https://lib.baomitu.com/tocbot/4.18.2/tocbot.min.js', function() {
    var toc = jQuery('#toc');
    if (toc.length === 0 || !window.tocbot) { return; }
    var boardCtn = jQuery('#board-ctn');
    var boardTop = boardCtn.offset().top;

    window.tocbot.init({
      tocSelector     : '#toc-body',
      contentSelector : '.markdown-body',
      headingSelector : CONFIG.toc.headingSelector || 'h1,h2,h3,h4,h5,h6',
      linkClass       : 'tocbot-link',
      activeLinkClass : 'tocbot-active-link',
      listClass       : 'tocbot-list',
      isCollapsedClass: 'tocbot-is-collapsed',
      collapsibleClass: 'tocbot-is-collapsible',
      collapseDepth   : CONFIG.toc.collapseDepth || 0,
      scrollSmooth    : true,
      headingsOffset  : -boardTop
    });
    if (toc.find('.toc-list-item').length > 0) {
      toc.css('visibility', 'visible');
    }
  });
</script>


  <script src=https://lib.baomitu.com/clipboard.js/2.0.10/clipboard.min.js></script>

  <script>Fluid.plugins.codeWidget();</script>


  
<script>
  Fluid.utils.createScript('https://lib.baomitu.com/anchor-js/4.3.1/anchor.min.js', function() {
    window.anchors.options = {
      placement: CONFIG.anchorjs.placement,
      visible  : CONFIG.anchorjs.visible
    };
    if (CONFIG.anchorjs.icon) {
      window.anchors.options.icon = CONFIG.anchorjs.icon;
    }
    var el = (CONFIG.anchorjs.element || 'h1,h2,h3,h4,h5,h6').split(',');
    var res = [];
    for (var item of el) {
      res.push('.markdown-body > ' + item.trim());
    }
    if (CONFIG.anchorjs.placement === 'left') {
      window.anchors.options.class = 'anchorjs-link-left';
    }
    window.anchors.add(res.join(', '));
  });
</script>


  
<script>
  Fluid.utils.createScript('https://lib.baomitu.com/fancybox/3.5.7/jquery.fancybox.min.js', function() {
    Fluid.plugins.fancyBox();
  });
</script>


  <script>Fluid.plugins.imageCaption();</script>

  <script  src="/my-blog/js/local-search.js" ></script>

  <script defer src="https://busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js" ></script>





<!-- 主题的启动项，将它保持在最底部 -->
<!-- the boot of the theme, keep it at the bottom -->
<script  src="/my-blog/js/boot.js" ></script>


  

  <noscript>
    <div class="noscript-warning">博客在允许 JavaScript 运行的环境下浏览效果更佳</div>
  </noscript>
</body>
</html>
